//
//  LongImageView.m
//  Wifi
//
//  Created by srulos on 2017/6/13.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "LongImageView.h"
#import "FLAnimatedImage.h"

@implementation LongImageView

- (id)initWithData:(NSData *)imageData isGif:(BOOL)isGif
{
    UIImage * image = [UIImage imageWithData:imageData];
    
    CGRect mainRect = [UIScreen mainScreen].bounds;
    if (self = [super initWithFrame:CGRectMake(0, 0,mainRect.size.width, mainRect.size.height)]) {
        CGSize imageSize = image.size;
        
        //偏移量
        CGFloat offsetScale = imageSize.width / mainRect.size.width;
        imageSize.height = imageSize.height / offsetScale;
        
        self.contentSize = CGSizeMake(mainRect.size.width, imageSize.height);
        
        if (isGif) {
            CGFloat yPos = 0.0f;
            if (imageSize.height < mainRect.size.height) {
                yPos = (mainRect.size.height - imageSize.height) / 2;
            }
            
            FLAnimatedImage * animateImage = [[FLAnimatedImage alloc]initWithAnimatedGIFData:imageData];
            
            FLAnimatedImageView * animateImageView = [[FLAnimatedImageView alloc]initWithFrame:CGRectMake(0, yPos, mainRect.size.width, imageSize.height)];
            animateImageView.animatedImage = animateImage;
            
            [self addSubview:animateImageView];
        }
        else{
            CGFloat yPos = 0.0f;
            if (imageSize.height < mainRect.size.height) {
                yPos = (mainRect.size.height - imageSize.height) / 2;
            }
            
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, yPos, mainRect.size.width, imageSize.height)];
            [self addSubview:imageView];
            
            image = [self compressOriginalImage:image toSize:CGSizeMake(mainRect.size.width, imageSize.height)];
            
            [imageView setImage:image];
        }
        
        [self setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    }
    return self;
}

-(UIImage *)compressOriginalImage:(UIImage *)image toSize:(CGSize)size{
    UIImage * resultImage = image;
    UIGraphicsBeginImageContext(size);
    [resultImage drawInRect:CGRectMake(00, 0, size.width, size.height)];
    UIGraphicsEndImageContext();
    return image;
}

- (void)onCloseLongImageView:(UIButton*)button
{
    [button removeFromSuperview];
    [self removeFromSuperview];
}

+(id)showInWindow:(NSData *)imageData isGif:(BOOL)isGif
{
    LongImageView * longImageView = [[LongImageView alloc]initWithData:imageData isGif:isGif];
    
    [[UIApplication sharedApplication].keyWindow addSubview:longImageView];
    
    UIButton * closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
    [closeButton setImage:[UIImage imageNamed:@"closeImage"] forState:UIControlStateNormal];
    [closeButton addTarget:longImageView action:@selector(onCloseLongImageView:) forControlEvents:UIControlEventTouchUpInside];
    
    [[UIApplication sharedApplication].keyWindow addSubview:closeButton];
    
    return longImageView;
}

@end
